<!--登录记录-->
<template>
  <div id="systemLog" class="app-container">
    <!--搜索条件-->
    <div class="filter-container">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="时间">
          <el-date-picker v-model="dateTime" type="datetimerange" value-format="timestamp" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" plain @click="handleFilter">搜索</el-button>
          <el-button icon="el-icon-refresh" plain @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-container flex-main">
      <el-table
        ref="multipleTable"
        :data="loginLogs"
        header-cell-class-name="table-header"
        fit
        height="100%"
        highlight-current-row
        @sort-change="sortChange">
        <el-table-column align="center" prop="id" label="#" width="60">
          <template slot-scope="scope">{{ scope.$index+1 }}</template>
        </el-table-column>
        <el-table-column label="状态" prop="isSuccess" align="center" width="70">
          <template slot-scope="scope">
            <span v-if="scope.row.isSuccess==1" class="color-primary">成功</span>
            <span v-if="scope.row.isSuccess==0" class="color-danger">失败</span>
          </template>
        </el-table-column>
        <el-table-column prop="gmtCreated" sortable label=" 登录时间" width="150">
          <template slot-scope="scope">{{ scope.row.gmtCreated | formatDate }}</template>
        </el-table-column>
        <el-table-column label="登录方式" prop="loginType" align="center" width="100" />
        <el-table-column label="登录地" prop="loginAddress" align="left" min-width="260">
          <template slot-scope="scope">
            <span v-if="scope.row.loginAddress"> {{ scope.row.loginAddress }}</span>
            <span v-if="scope.row.loginIp" class="color-primary"> ({{ scope.row.loginIp }})</span>
          </template>
        </el-table-column>
        <el-table-column label="浏览器" prop="brower" min-width="190" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.browser }}<span v-if="scope.row.browser" class="color-primary"> ({{ scope.row.browserVersion }})</span>
          </template>
        </el-table-column>
        <el-table-column label="操作系统" prop="os" min-width="140" />

        <el-table-column label="操作描述" prop="remark" min-width="200" show-overflow-tooltip />
      </el-table>
    </div>
    <div class="pagination_handler">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
    </div>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  components: { Pagination },

  data() {
    return {
      systemLogs: null,
      loginLogs: null,
      systemLog: {},
      total: 0,
      listQuery: {
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        sort: this.common.SORT_DEFAULT
      },
      dateTime: [],

      multipleSelectionRecords: [],
      dialogVisible: false
    }
  },
  watch: {

  },
  created() {
    this.fetchData()
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    async fetchData() {
      if (this.dateTime.length > 0) {
        this.listQuery.startTime = this.dateTime[0]
        this.listQuery.endTime = this.dateTime[1]
      }
      const response = await this.$store.dispatch('currentUserLoginHistoryPage', this.listQuery)
      this.loginLogs = response.records
      this.total = response.total
    },
    /**
     * 全选/反选
     */
    handleSelectionChange(val) {
      this.multipleSelectionRecords = val
    },

    // 固定写法
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = this.common.SORT_DEFAULT
      }
      this.handleFilter()
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    handleRowClick(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    /**
     * 重置
     */
    handleReset() {
      this.dateTime = []
      this.listQuery.startTime = ''
      this.listQuery.endTime = ''
      this.listQuery.sort = this.common.SORT_DEFAULT
      this.handleFilter()
    }
  }
}
</script>

<style lang="scss" >
#systemLog{
  .el-tabs{
    height: 100%;
  }
  .el-tabs__nav-scroll{
    padding-top: 0px !important;
  }
  .el-tabs__content {
    height: calc(100% - 40px);
    padding: 0;
    .el-tab-pane {
      height: 100%;
      overflow: hidden;
      display: flex;
      flex-direction: column;
    }
  }
}
</style>
